package Distributive;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class playerProxy implements Runnable {

	private IBuffer sharedBuffer;
	private Socket clientSocket;

	private ObjectOutputStream output;

	/**
	 * constructor for the player Proxy
	 * 
	 * @param clientSocket
	 *            the socket of the client
	 * @param shared
	 *            the shared IBuffer
	 */
	public playerProxy(Socket clientSocket, IBuffer shared) {
		this.clientSocket = clientSocket;
		this.sharedBuffer = shared;

		try {
			this.output = new ObjectOutputStream(this.clientSocket
					.getOutputStream());
		} catch (IOException e) {
			System.exit(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {

		String valueFromBuffer = "";

		while (!valueFromBuffer.equalsIgnoreCase("bye")) {
			valueFromBuffer = this.sharedBuffer.getValue();
			try {
				this.output.writeObject(valueFromBuffer);
			} catch (IOException e) {
				System.exit(0);
			}
		}

		System.out.println("Consumer at: " + this.clientSocket.getInetAddress()
				+ " ends");
	}

}
